define([
    "helpers/network",
    "views/modules/popup",
    "views/modules/form",
    "libs/md5"
], function (Network, PopupView, CommonForm, md5) {
    var windowId = "windowId";
    var formId = "formId";
    var closeWindow = function () {
        if ($$(windowId)) {
            $$(windowId).close();
        }
    };

    var onSubmit = function () {
        var userid = sessionStorage.getItem("userid");
        if (!userid) {
            webix.message("系统错误, 请重新登录");
            return
        }
        var oldpwd = $$(formId).getValues().oldPwd;
        var newpwd = $$(formId).getValues().newPwd;
        var obj = {
            data: {
                id:userid,
                oldPassword: md5(oldpwd),
                password: md5(newpwd),
                lastModifyTime: new Date()
            }
        };
        Network.ajax("/AdminTransactor.updateMyPassword.command", obj, function (ret) {
            closeWindow();
            sessionStorage.clear();
            window.location.href = ''
        })
    };

    var changePwd = function (title) {
        PopupView(windowId, title, {
            rows: [{
                view: "form", borderless: true, id: formId,
                elements: [
                    {
                        name: "oldPwd", view: "text", label: "原密码", type: "password", required: true
                    },
                    {
                        name: "newPwd", view: "text", label: "新密码",type: "password", required: true
                    },
                    // {name: "rePwd", view: "text", label: "新密码", value: item.data.remark || ""},
                ]
            },
                CommonForm.genSubmitButton(function () {
                    onSubmit()
                })]
        }, 300, 250)
    };

    var logout = function () {
        sessionStorage.clear();
        window.location.href = ''
    };

    return {
        $ui: {
            view: "submenu", id: "profilePopup", width: 200, padding: 0,
            data: [
                // {id: 1, icon: "user", value: "My Profile"},
                // {id: 2, icon: "cog", value: "My Account"},
                // {id: 3, icon: "calendar", value: "My Calendar"},
                {id: 5, icon: "tasks", value: "修改密码"},
                {$template: "Separator"},
                {id: 4, icon: "sign-out", value: "退出登录"}
            ],
            on: {
                onItemClick: function (id, event, node) {
                    if (this.getItem(id).value === "退出登录") {
                        logout()
                    } else if (this.getItem(id).value === "修改密码") {
                        changePwd(this.getItem(id).value)
                    }
                }
            },
            type: {
                template: function (obj) {
                    if (obj.type)
                        return "<div class='separator'></div>";
                    return "<span class='webix_icon alerts fa-" + obj.icon + "'></span><span>" + obj.value + "</span>";
                }
            }

        }
    };

});